const CHAT_ID_REGEXP = /\/(\d+)$/;

module.exports = (redis) => {
  return async (request, response) => {
    const matches = CHAT_ID_REGEXP.exec(request.url);
    if(!matches) return response.end('{"error": "no chat id"}');
    const chatId = matches[1];
    const key = `c:${chatId}`;

    const result = await redis.ratelimit(chatId);
    const isBlocked = result <= 0;
    return response.end(`{"is_blocked": ${isBlocked}}`);
  }
}